package com.coolgor.coldot.dao.wechat.gzh;

import com.coolgor.coldot.entity.wechat.gzh.GZHAccessToken;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface GZHAccessTokenDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_gzh_accesstoken
			WHERE
				record_id = #{recordID}	
		</script>		
	""")
	@Results(id = "GZHAccessTokenMap", value = {
			@Result(property = "recordID", column = "record_id"),
			@Result(property = "appID", column = "app_id"),
			@Result(property = "appName", column = "app_name"),
			@Result(property = "secret", column = "secret"),
			@Result(property = "accessToken", column = "access_token"),
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "invalidTime", column = "invalid_time"),
			@Result(property = "expireTime", column = "expire_time")
	})
	GZHAccessToken queryByRecordID(Long recordID);

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_gzh_accesstoken
			WHERE
				app_id = #{appID}	
		</script>		
	""")
	@ResultMap("GZHAccessTokenMap")
	GZHAccessToken queryByAppID(String appID);


	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_gzh_accesstoken
			<where>
				<if test="gzhAccessTokenCondition.recordID != null">
					and record_id =	#{gzhAccessTokenCondition.recordID}
				</if>
				<if test="gzhAccessTokenCondition.appID != null">
					and app_id = #{gzhAccessTokenCondition.appID}
				</if>
				<if test="gzhAccessTokenCondition.appName != null">
					and app_name like '%${gzhAccessTokenCondition.appName}%'
				</if>
			</where>
		</script>		
	""")
	@ResultMap("GZHAccessTokenMap")
	List<GZHAccessToken> queryList(@Param("gzhAccessTokenCondition") GZHAccessToken gzhAccessTokenCondition);


	@Insert("""
		<script>
		INSERT INTO tb_coldot_wechat_gzh_accesstoken
			(app_id,app_name,secret,access_token,create_time,invalid_time,expire_time)
		VALUES
			(#{appID},#{appName},#{secret},#{accessToken},#{createTime},#{invalidTime},#{expireTime})	
		</script>		
	""")
	@Options(useGeneratedKeys = true, keyColumn = "record_id", keyProperty = "recordID")
	int insert(GZHAccessToken gzhAccessToken);


	@Update("""
		<script>
			update tb_coldot_wechat_gzh_accesstoken
			<set>
				<if test="appID != null">app_id=#{appID},</if>
				<if test="appName != null">app_name=#{appName},</if>
				<if test="secret != null">secret=#{secret},</if>
				<if test="accessToken != null">access_token=#{accessToken},</if>
				<if test="createTime != null">create_time=#{createTime},</if>
				<if test="invalidTime != null">invalid_time=#{invalidTime},</if>
				<if test="expireTime != null">expire_time=#{expireTime}</if>
			</set>
			where record_id=#{recordID}	
		</script>		
	""")
	int update(GZHAccessToken gzhAccessToken);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_wechat_gzh_accesstoken
			WHERE
				record_id=#{recordID}	
		</script>		
	""")
	int delete(GZHAccessToken gzhAccessToken);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_wechat_gzh_accesstoken
			WHERE record_id IN
			<foreach collection="list" item="recordID" open="(" separator="," close=")">
				#{recordID}
			</foreach>
		</script>	
	""")
	int deleteBatch(List<Long> recordIDList);

}
